package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.oa.entity.SysDictItemEntity;
import org.edu.modules.oa.entity.VeFileFilesEntity;
import org.edu.modules.oa.entity.VeOaCarInfoEntity;
import org.edu.modules.oa.entity.VeOaCarJiayouEntity;
import org.edu.modules.oa.mapper.ISysDictItemMapper;
import org.edu.modules.oa.mapper.IVeFileFilesMapper;
import org.edu.modules.oa.mapper.IVeOaCarInfoMapper;
import org.edu.modules.oa.mapper.IVeOaCarJiayouMapper;
import org.edu.modules.oa.service.IVeOaCarInfoService;
import org.edu.modules.oa.service.IVeOaCarJiayouService;
import org.edu.modules.oa.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class VeOaCarInfoServiceImpI extends ServiceImpl<IVeOaCarInfoMapper, VeOaCarInfoEntity>  implements IVeOaCarInfoService {

    @Resource
    private  IVeOaCarInfoMapper iVeOaCarInfoMapper;

    @Resource
    private IVeFileFilesMapper veFileFilesMapper;

    @Resource
    private ISysDictItemMapper iSysDictItemMapper; // 字典Mapper
    @Override
    public HashMap<String, Object> showTable(VeOaCarInfoEntity veOaCarInfoEntity, Integer startPage, Integer pageSize) {
        if( !veOaCarInfoEntity.getCarName().equals("")){
            veOaCarInfoEntity.setCarName("'%"+veOaCarInfoEntity.getCarName()+"%'");

        }
        if( !veOaCarInfoEntity.getCarPaihao().equals("")){
            veOaCarInfoEntity.setCarPaihao("'%"+veOaCarInfoEntity.getCarPaihao()+"%'");

        }
        if( !veOaCarInfoEntity.getLeixing().equals("")){
            veOaCarInfoEntity.setLeixing("'%"+veOaCarInfoEntity.getLeixing()+"%'");

        }
        if( !veOaCarInfoEntity.getSuoshuDep().equals("")){
            veOaCarInfoEntity.setSuoshuDep("'%"+veOaCarInfoEntity.getSuoshuDep()+"%'");

        }
        if( !veOaCarInfoEntity.getDriverUser().equals("")){
            veOaCarInfoEntity.setDriverUser("'%"+veOaCarInfoEntity.getDriverUser()+"%'");

        }


        List<VeOaCarInfoTableVo> voelist=iVeOaCarInfoMapper.showTable((startPage-1)*pageSize,pageSize,veOaCarInfoEntity);
        int count =iVeOaCarInfoMapper.tableCount(veOaCarInfoEntity);
        // 把查出来的东西存到map里面去
        HashMap<String , Object> map = new HashMap<>();
        map.put("list",voelist);
        map.put("count",count);

        // 查询表格总行数
        return map;
    }

    @Override
    public Integer deleteRecord(Integer id) {
        int result = iVeOaCarInfoMapper.deleteById(id);
        return result;
    }

    @Override
    public List<VeOaCarInfoExportVo> xporte() {
        // 拼接查询条件
        QueryWrapper<VeOaCarInfoEntity> tableWrapper = new QueryWrapper<>();
        tableWrapper.eq("is_deleted",0);

        // 查询加油表的所有数据
        List<VeOaCarInfoEntity> entitylist =iVeOaCarInfoMapper.selectList(tableWrapper);

        // 表格集合的VO
        List<VeOaCarInfoExportVo> voList=new ArrayList<>();
        // 把查出来的东西加入到表格VO里面去
        for(VeOaCarInfoEntity entity:entitylist){
            VeOaCarInfoExportVo vo =new VeOaCarInfoExportVo();
            vo.setCarName(entity.getCarName());
            vo.setCarPaihao(entity.getCarPaihao());
            vo.setCarXinghao(entity.getCarXinghao());
//            -----------------------修改导出数据（林彬辉）
            SysDictItemEntity dictItemEntity = iSysDictItemMapper.selectById(entity.getLeixing());//获取id对应的类型名称
            vo.setLeixing(dictItemEntity.getItemText());
//            ---------------------------
//            vo.setLeixing(entity.getLeixing());

            vo.setSuoshuDep(entity.getSuoshuDep());
            vo.setDriverUser(entity.getDriverUser());
//----------------------------------------修改导出数据（林彬辉）
            SysDictItemEntity sysDictItemEntity =  iSysDictItemMapper.selectById(entity.getNowStatus());//获取id对应的状态名称
            vo.setNowStatus(sysDictItemEntity.getItemText());
//            ----------------------------
//            vo.setNowStatus(entity.getNowStatus());

            vo.setBackInfo(entity.getBackInfo());
            vo.setTixingContent(entity.getTixingContent());

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            vo.setTixingDate(formatter.format(entity.getTixingDate()));
            vo.setTixingUser(entity.getTixingUser());
            voList.add(vo);

        }
        return  voList;
    }


    @Override
    public VeOaCarInfoEntity getCarId(String name) {
        // 查找该车辆有没有 没有就提醒用户
        QueryWrapper<VeOaCarInfoEntity> tableWrapper = new QueryWrapper<>();
        System.out.println(name);
        tableWrapper.eq("car_name",name);
        return   iVeOaCarInfoMapper.selectOne(tableWrapper);
    }

    @Override
    public VeOaCarInfoEntity getCarName(Long id) {

        return   iVeOaCarInfoMapper.selectById(id);
    }

    @Override
    public List<VeOaCarInfoEntity> getCarBox() {
        // 获取所有车辆名字的下拉框
        QueryWrapper<VeOaCarInfoEntity> tableWrapper = new QueryWrapper<>();
        return   iVeOaCarInfoMapper.selectList(tableWrapper);
    }

    @Override
    public VeOaCarInfoShowVo viewDetails(Integer id) {
        VeOaCarInfoEntity entity=  iVeOaCarInfoMapper.selectById(id);
        VeOaCarInfoShowVo showVo=new VeOaCarInfoShowVo();

        showVo.setCarName(entity.getCarName());
        showVo.setCarPaihao(entity.getCarPaihao());
        showVo.setCarXinghao(entity.getCarXinghao());
        showVo.setLeixing(entity.getLeixing());
        showVo.setSuoshuDep(entity.getSuoshuDep());
        showVo.setDriverUser(entity.getDriverUser());
        showVo.setNowStatus(entity.getNowStatus());
        showVo.setBackInfo(entity.getBackInfo());
        showVo.setTixingContent(entity.getTixingContent());
        // 时间类型转换成字符串
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        showVo.setTixingDate(formatter.format(entity.getTixingDate()));
        showVo.setTixingUser(entity.getTixingUser());
        showVo.setCreateTime(formatter.format(entity.getCreateTime()));
        showVo.setCreateByName(entity.getCreateByName());


        return showVo ;
    }

    @Override
    public Integer amendCarInfo(VeOaCarInfoEntity veOaCarInfoEntity, VeFileFilesEntity veFileFilesEntity) throws  Exception   {
        QueryWrapper<VeOaCarInfoEntity> tableWrapper = new QueryWrapper<>();
        tableWrapper.eq("id",veOaCarInfoEntity.getId());
        Integer reust=0;
        if(veFileFilesEntity == null){
            reust=  iVeOaCarInfoMapper.update(veOaCarInfoEntity,tableWrapper);
        }
        if(veFileFilesEntity != null) {
            int add = veFileFilesMapper.insert(veFileFilesEntity);

            if(add != 0){
                veOaCarInfoEntity.setFileId(veFileFilesEntity.getId());

                reust=  iVeOaCarInfoMapper.update(veOaCarInfoEntity,tableWrapper);
            }



        }

        return reust;
    }

    @Override
    public Integer addCarInfo(VeOaCarInfoEntity veOaCarInfoEntity, VeFileFilesEntity veFileFilesEntity) {
        int res = 0;
        if(veFileFilesEntity == null){
            Integer reust= iVeOaCarInfoMapper.insert(veOaCarInfoEntity);
        }
        if(veFileFilesEntity != null){
            int add = veFileFilesMapper.insert(veFileFilesEntity);
            if(add != 0){
                veOaCarInfoEntity.setFileId(veFileFilesEntity.getId());
                Integer reust= iVeOaCarInfoMapper.insert(veOaCarInfoEntity);
            }
        }
        return res;

    }

    @Override
    public VeOaCarInfoEntity duplicationCheck(String carName, String numPlate) {


        QueryWrapper<VeOaCarInfoEntity> tableWrapper = new QueryWrapper<>();

        tableWrapper.eq("car_name",carName);

        return iVeOaCarInfoMapper.selectOne(tableWrapper);
    }
    @Override
    public HashMap<String, Object> advancedQuery(String advancedQuery) throws Exception {
        // 把查出来的东西存到map里面去
        HashMap<String , Object> map = new HashMap<>();
        List<VeOaCarInfoEntity> entityList= iVeOaCarInfoMapper.advancedQuery(advancedQuery);
        Integer count= iVeOaCarInfoMapper.advancedQueryCount(advancedQuery);

        List<VeOaCarInfoTableVo> voelist =new ArrayList<>();
        // for循环把需要用的字段加到 导出集合里面去
        for (VeOaCarInfoEntity o:entityList){
            VeOaCarInfoTableVo tableVo =new VeOaCarInfoTableVo();
            tableVo.setId(o.getId());
            tableVo.setCarName(o.getCarName());
            tableVo.setCarPaihao(o.getCarPaihao());
            tableVo.setCarXinghao(o.getCarXinghao());
            tableVo.setLeixing(o.getLeixing());
            tableVo.setSuoshuDep(o.getSuoshuDep());
            tableVo.setDriverUser(o.getDriverUser());
            tableVo.setNowStatus(o.getNowStatus());
            voelist.add(tableVo);


        }

        map.put("list",voelist);
        map.put("count",count);

        // 查询表格总行数
        return map;

    }

    @Override
    public HashMap<String, Object> loadBox() {
        QueryWrapper<SysDictItemEntity> typeWrapper = new QueryWrapper();//类型类别查询对象
        typeWrapper.eq("dict_id","1386861063413415938");
        QueryWrapper<SysDictItemEntity> stateWrapper = new QueryWrapper();//状态类别查询对象
        stateWrapper.eq("dict_id","1386863928907382786");



        List<SysDictItemEntity> typeList=   iSysDictItemMapper.selectList(typeWrapper);
        List<SysDictItemEntity> stateList=   iSysDictItemMapper.selectList(stateWrapper);
        HashMap<String,Object> map = new HashMap<>();
        map.put("typeList",typeList);
        map.put("stateList",stateList);

        return map;
    }
}
